package com.adtec.yxdemo.filelanding.service;

import com.adtec.yxdemo.utils.FileLandUtil;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 文件落地
 */

public class YxPayInfoFileService {
    private static Logger logger = Logger.getLogger(YxPayInfoFileService.class); // 获取logger实例
    @Inject
    SelectDbService selectDbService;
    private static final String PATH1 = "D:\\001\\003\\MC001.dat";
    private static final String PATH2 = "D:\\001\\003\\MC002.dat";
    private static final String path2 = "D:\\001\\003\\MC002.ind";
    private static final String PATH3 = "D:\\001\\003\\MC003.dat";
    private static final String path3 = "D:\\001\\003\\MC003.ind";
    private static final String PATH4 = "D:\\001\\003\\MC004.dat";
    private static final String path4 = "D:\\001\\003\\MC004.ind";
    private static final String PATH5 = "D:\\001\\003\\MC005.dat";
    private static final String path5 = "D:\\001\\003\\MC005.ind";
    //account
    String[] FILED1 = {""};
    //payInfo
    String[] FILED2 = {"pay_order_code", "pay_org_name", "pay_order_amount", "warehouse_amount", "return_amount",
            "audit_status", "pay_status", "add_user_name", "add_time"};
    //payTrack
    String[] FILED5 = {"pay_order_code", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8"};
    //payDetail
    String[] FILED4 = {"pay_order_code", "goods_id", "name_code", "produce_info", "brand", "unit", "company_name", "invoice_id",
            "lastSync_time", "warehouse_count", "real_amount", "storage_tag"};
    //payOrderDetil
    String[] FILED3 = {"pay_order_code", "order_name", "harvest_amount", "return_amount", "atatement_amount", "payment_company",
            "receiving_company", "payment_account", "receiving_account", "payment_union_bank_num", "receivinh_union_bank_num",
            "payment_bank_name", "receiving_bank_name", "loan_account", "loan_account_name"};

    public void svaeYxAccount() {
        List<Record> accountInfos = selectDbService.getAccountInfo();
        try {
            FileLandUtil.documentSave(accountInfos, PATH1, FILED1);
        } catch (IOException e) {
            logger.warn("用户信息文件保存失败");
        }
    }

    /**
     * 存储payInfo
     *
     * @throws IOException
     */
    public void saveYxPayInfo() {
        List<Record> payInfos = selectDbService.getPayInfos();
        File file = null;
        try {
            file = FileLandUtil.documentSave(payInfos, PATH2, FILED2);
        } catch (IOException e) {
            logger.warn("payInfo信息文件保存失败");
        }
        try {
            FileLandUtil.fileSaveFlag(path2, file, payInfos.size());
        } catch (IOException e) {
            logger.warn("payInfo标志文件生成失败");
        }
    }

    /**
     * 存储payTrack
     *
     * @throws IOException
     */
    public void saveYxPayTrack() {
        List<Record> payTracks = selectDbService.getPayTracks();
        File file = null;
        try {
            file = FileLandUtil.documentSave(payTracks, PATH5, FILED5);
        } catch (IOException e) {
            logger.warn("payTrack信息文件生成失败");
        }
        try {
            FileLandUtil.fileSaveFlag(path5, file, payTracks.size());
        } catch (IOException e) {
            logger.warn("payTrack标志文件生成失败");
        }
    }

    /**
     * 存储yxOrderDetails
     */
    public void savaYxOrderDetails() {
        List<Record> orderDetails = selectDbService.getOrderDetails();
        File file = null;
        try {
            file = FileLandUtil.documentSave(orderDetails, PATH3, FILED3);
        } catch (IOException e) {
            logger.warn("orderDetail信息文件生成失败");
        }
        try {
            FileLandUtil.fileSaveFlag(path3, file, orderDetails.size());
        } catch (IOException e) {
            logger.warn("orderDetail标志文件生成失败");
        }
    }


    /**
     * 存储yxDetails
     */
    public void savaYxDetails() {
        List<Record> details = selectDbService.getDetails();
        File file = null;
        try {
            file = FileLandUtil.documentSave(details, PATH4, FILED4);
        } catch (IOException e) {
            logger.warn("detail信息文件生成失败");
        }
        try {
            FileLandUtil.fileSaveFlag(path4, file, details.size());
        } catch (IOException e) {
            logger.warn("detail标志文件生成失败");
        }
    }


}
